#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

struct tokenizer {
	char * mark;
	char * scan;
};

enum token_result {
	TOKEN_VALID  = 0,
	TOKEN_FINISH = 1,
	TOKEN_ERROR  = 2,
};

struct token {
	enum token_result result;
	size_t size;
	const char * text;
};

static void tokenizer_create(struct tokenizer * tokenizer,
			     char * buffer)
{
	tokenizer->mark = NULL;
	tokenizer->scan = buffer;
}

static void tokenizer_getnext(struct tokenizer * tokenizer,
			      struct token * token)
{
	char * mark, * scan;
	enum token_result result;
	
	mark = tokenizer->mark;
	scan = tokenizer->scan;
	
	for (;; scan++) {
		char here;
		
		here = *scan;
		if (isalpha(here)) {
			here = tolower(here);
			*scan = here;
			if (mark == NULL)
				mark = scan;
			continue;
		}
		if (isspace(here)) {
			if (mark != NULL) {
				*scan = '\0';
				result = TOKEN_VALID;
				break;
			} else {
				continue;
			}
		}
		if (here == '\0') {
			if (mark == NULL)
				result = TOKEN_FINISH;
			else
				result = TOKEN_VALID;
			break;
		} else {
			result = TOKEN_ERROR;
			break;
		}
	}
	
	token->result = result;
	token->size = scan - mark;
	token->text = mark;
	
	scan++;
	
	tokenizer->mark = NULL;
	tokenizer->scan = scan;
}

enum keyword_type {
	KEYWORD_NEGATE = 0,
	KEYWORD_NUMBER = 1,
	KEYWORD_SCALE  = 2,
	KEYWORD_SHIFT  = 3,
};

struct keyword {
	const char * text;
	enum keyword_type type;
	int value;
};

struct keyword_list {
	const struct keyword * array;
	unsigned int count;
};

static const struct keyword keywords_3[] = {
	{ "one", KEYWORD_NUMBER, 1 },
	{ "six", KEYWORD_NUMBER, 6 },
	{ "ten", KEYWORD_NUMBER, 10 },
	{ "two", KEYWORD_NUMBER, 2 },
};

static const struct keyword keywords_4[] = {
	{ "five", KEYWORD_NUMBER, 5 },
	{ "four", KEYWORD_NUMBER, 4 },
	{ "nine", KEYWORD_NUMBER, 9 },
	{ "zero", KEYWORD_NUMBER, 0 },
};

static const struct keyword keywords_5[] = {
	{ "eight", KEYWORD_NUMBER, 8 },
	{ "fifty", KEYWORD_NUMBER, 50 },
	{ "forty", KEYWORD_NUMBER, 40 },
	{ "minus", KEYWORD_NEGATE, 0 },
	{ "seven", KEYWORD_NUMBER, 7 },
	{ "sixty", KEYWORD_NUMBER, 60 },
	{ "three", KEYWORD_NUMBER, 3 },
};

static const struct keyword keywords_6[] = {
	{ "eighty", KEYWORD_NUMBER, 80 },
	{ "eleven", KEYWORD_NUMBER, 11 },
	{ "fourty", KEYWORD_NUMBER, 40 },
	{ "ninety", KEYWORD_NUMBER, 90 },
	{ "thirty", KEYWORD_NUMBER, 30 },
	{ "twelve", KEYWORD_NUMBER, 12 },
	{ "twenty", KEYWORD_NUMBER, 20 },
};

static const struct keyword keywords_7[] = {
	{ "billion", KEYWORD_SCALE,  1000000000 },
	{ "fifteen", KEYWORD_NUMBER, 15 },
	{ "hundred", KEYWORD_SHIFT,  100 },
	{ "million", KEYWORD_SCALE,  1000000 },
	{ "seventy", KEYWORD_NUMBER, 70 },
	{ "sixteen", KEYWORD_NUMBER, 16 },
};

static const struct keyword keywords_8[] = {
	{ "eighteen", KEYWORD_NUMBER, 18 },
	{ "fourteen", KEYWORD_NUMBER, 14 },
	{ "negative", KEYWORD_NEGATE, 0 },
	{ "nineteen", KEYWORD_NUMBER, 19 },
	{ "thirteen", KEYWORD_NUMBER, 13 },
	{ "thousand", KEYWORD_SCALE,  1000 },
};

static const struct keyword keywords_9[] = {
	{ "seventeen", KEYWORD_NUMBER, 17 },
};

static const struct keyword_list recognized_keywords[] = {
	{ keywords_3, sizeof(keywords_3) / sizeof(*keywords_3) },
	{ keywords_4, sizeof(keywords_4) / sizeof(*keywords_4) },
	{ keywords_5, sizeof(keywords_5) / sizeof(*keywords_5) },
	{ keywords_6, sizeof(keywords_6) / sizeof(*keywords_6) },
	{ keywords_7, sizeof(keywords_7) / sizeof(*keywords_7) },
	{ keywords_8, sizeof(keywords_8) / sizeof(*keywords_8) },
	{ keywords_9, sizeof(keywords_9) / sizeof(*keywords_9) },
};

static int keyword_search(const void * lookup,
			  const void * member)
{
	return strcmp((const char *)lookup,
		      ((const struct keyword *)member)->text);
}

static const struct keyword * token_keyword(const struct token * token)
{
	size_t size;
	const char * text;
	
	size = token->size;
	text = token->text;
	
	if (size < 3)
		return NULL;
	size -= 3;
	if (size > sizeof(recognized_keywords) / sizeof(*recognized_keywords))
		return NULL;
	
	return bsearch(text,
		       recognized_keywords[size].array,
		       recognized_keywords[size].count,
		       sizeof(*recognized_keywords->array),
		       keyword_search);
}

enum number_status {
	NUMBER_FIRST  = 0x1,
	NUMBER_LAST   = 0x2,
	NUMBER_NEGATE = 0x4,
};

struct number_component {
	int scale;
	int value;
};

struct number {
	enum number_status status;
	struct number_component accum;
	struct number_component total;
};

static void number_create(struct number * number)
{
	number->status = NUMBER_FIRST;
	number->accum.scale = 0;
	number->accum.value = 0;
	number->total.scale = 0;
	number->total.value = 0;
}

static int number_append(struct number * number,
			 const struct keyword * keyword)
{
	int newval;
	
	if ((number->status & NUMBER_LAST) != 0)
		return 1;
	
	switch (keyword->type) {
	case KEYWORD_NEGATE:
		if ((number->status & NUMBER_FIRST) == 0)
			return 1;
		number->status |= NUMBER_NEGATE;
		break;
	case KEYWORD_NUMBER:
		if (keyword->value != 0) {
			if (keyword->value >= 10)
				newval = 10;
			else
				newval = 1;
			if (number->accum.scale != 0 &&
			    number->accum.scale <= newval)
				return 1;
			if (keyword->value >= 20)
				newval = 10;
			else
				newval = 1;
			number->accum.scale = newval;
			number->accum.value += keyword->value;
		} else {
			if ((number->status & NUMBER_FIRST) == 0)
				return 1;
			number->status |= NUMBER_LAST;
		}
		break;
	case KEYWORD_SCALE:
		if (number->total.scale != 0 &&
		    number->total.scale <= keyword->value)
			return 1;
		if (number->accum.value == 0)
			return 1;
		number->accum.value *= keyword->value;
		number->total.scale = keyword->value;
		number->total.value += number->accum.value;
		number->accum.scale = 0;
		number->accum.value = 0;
		break;
	case KEYWORD_SHIFT:
		newval = number->accum.value * keyword->value;
		if (number->total.scale != 0 &&
		    number->total.scale <= newval)
			return 1;
		if (number->accum.value >= keyword->value)
			return 1;
		if (number->accum.value == 0)
			return 1;
		number->accum.scale = keyword->value;
		number->accum.value = newval;
		break;
	}
	
	number->status &= ~NUMBER_FIRST;
	return 0;
}

static int number_finish(const struct number * number)
{
	int result;
	
	result = number->accum.value +
		 number->total.value;
	if ((number->status & NUMBER_NEGATE) != 0)
		result = -result;
	
	return result;
}

int main(void)
{
	char * buffer;
	size_t length;
	struct tokenizer tokenizer;
	struct number number;
	int success;
	
	buffer = NULL;
	length = 0;
	if (getline(&buffer, &length, stdin) < 0)
		return 0;
	
	tokenizer_create(&tokenizer, buffer);
	number_create(&number);
	
	for (;;) {
		struct token token;
		const struct keyword * keyword;
		
		tokenizer_getnext(&tokenizer, &token);
		if (token.result != TOKEN_VALID) {
			success = token.result != TOKEN_ERROR;
			break;
		}
		
		keyword = token_keyword(&token);
		if (keyword == NULL) {
			success = 0;
			break;
		}
		
		if (number_append(&number, keyword)) {
			success = 0;
			break;
		}
	}
	
	if (success)
		printf("%d\n", number_finish(&number));
	else
		printf("NaN\n");
	return 0;
}
